#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: bruce
# Email: lixiangning888@gmail.com
# Description: script to convert data from mysql database to mongodb. For help please read README file.
# If any problem in the code, please sent an email

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import re
import getopt
import pprint
import encodings
import MySQLdb

import pymongo

pp = pprint.PrettyPrinter()

class convert_mysql_to_mongodb:

    def __init__(self):
        self.mysql_db = self.init_mysql()
        self.mongo_db = self.init_mongodb()

    def init_mysql(self):
        '''init mysql database connection'''
        conn = MySQLdb.connect(host="localhost",user="root",passwd="root",db="news")
        db = conn.cursor()
        return db

    def init_mongodb(self):
        '''init mongodb database connection'''
        connection = pymongo.Connection('localhost', 27017)
        db = connection.news
        return db

    def get_all_news_from_mysql(self):
        get_news_sql = '''select * from news'''
        self.mysql_db.execute(get_news_sql)
        return self.mysql_db.fetchall()

    def get_news_between_min_and_max(self,min_id):
        get_news_between_sql = "select * from news where id between %d and (select max(id) from news)" % int(min_id)
        self.mysql_db.execute(get_news_between_sql)
        return self.mysql_db.fetchall()
        

    def get_mini_id_news_from_mongodb(self):
        news = self.mongo_db.news
        get_news = news.find({},{'id':1}).sort('id',pymongo.DESCENDING).limit(1)
        #pp.pprint(get_news)
        return get_news[0]['id']

    def insert_into_mongodb(self,newset):
        news = self.mongo_db.news
        for items in newset:
            news_record = {"id": items[0], "title": items[1], "author": items[2], "time": items[3], "content": items[4], "tag": items[5], "rel_link": items[6]}
            news.insert(news_record)
            #pp.pprint(news)
            print news_record['id']
            print news_record['title']

    def import_all_news_from_mysql_to_mongodb(self):
        all_news = self.get_all_news_from_mysql()
        self.insert_into_mongodb(all_news)

    def incremental_sync_from_mysql_to_mongodb(self):
        min_id = self.get_mini_id_news_from_mongodb()
        all_news = self.get_news_between_min_and_max(min_id)
        self.insert_into_mongodb(all_news)


class opt_usage():
    def __init__(self,options):
        #pp.pprint(options)
        self.options = options

    def usage(self):
        print 'TOOLS Usage:'
        print '-a - To sync all the data from mysql to mongodb'
        print '-i - To increment sync data from mysql to mongodb'

    def argv_al(self,object_convert):
        if len(self.options) > 2:
            print 'Only one option accecpt at time! Please use `-a` or `-i`.'
            self.usage()
        elif "-a" in self.options and self.options[1] == '-a':
            object_convert.import_all_news_from_mysql_to_mongodb()
        elif "-i" in self.options and self.options[1] == '-i':
            object_convert.incremental_sync_from_mysql_to_mongodb()
        else:
            self.usage()
            
        
if __name__ == '__main__':
    option_usage = opt_usage(sys.argv)
    object_convert = convert_mysql_to_mongodb()
    option_usage.argv_al(object_convert)
    '''import all news from mysql to mongodb, TODO: add the option to choose all data sync or incremental sync'''
    #object_convert.import_all_news_from_mysql_to_mongodb()
    #object_convert.incremental_sync_from_mysql_to_mongodb()
